//Unit test for scoreFor()

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>

#define _NUM_TESTS 100

void fail(int n) {
    printf("FAIL !!!\n");
    exit(n);
}

int main() {
	srand(time(NULL));

	int i;
	int num_cards, num_curse, num_estate, num_duchy, 
        num_province, num_great_hall, num_garden;
	int calculated;
	int player = 0;
	struct gameState state;
	
	int test = 0;
	while(test++ != _NUM_TESTS) {
		calculated = 0, num_cards = 0, num_curse = 0, num_estate = 0, num_duchy = 0, 
        num_province = 0, num_great_hall = 0, num_garden = 0;
		
		state.handCount[player] = rand() % (MAX_HAND + 1);
		for (i = 0; i < state.handCount[player]; ++i) {
			state.hand[player][i] = rand() % 27;
			if (state.hand[player][i] == curse) { num_curse += 1; }
			if (state.hand[player][i] == estate) { num_estate +=1; }
			if (state.hand[player][i] == duchy) { num_duchy += 1; }
			if (state.hand[player][i] == province) { num_province +=1; }
			if (state.hand[player][i] == great_hall) {num_great_hall += 1; }
			if (state.hand[player][i] == gardens) { num_garden += 1; }
		}

		state.discardCount[player] = rand() % (MAX_DECK + 1);
		for (i = 0; i < state.discardCount[player]; ++i) {
			state.discard[player][i] = rand() % 27;
			if (state.discard[player][i] == curse) { num_curse += 1; }
			if (state.discard[player][i] == estate) { num_estate +=1; }
			if (state.discard[player][i] == duchy) { num_duchy += 1; }
			if (state.discard[player][i] == province) { num_province +=1; }
			if (state.discard[player][i] == great_hall) {num_great_hall += 1; }
			if (state.discard[player][i] == gardens) { num_garden += 1; }
		}

		state.deckCount[player] = rand() % (MAX_DECK + 1);
		for (i = 0; i < state.deckCount[player]; ++i) {
			state.deck[player][i] = rand() % 27;
			if (state.deck[player][i] == curse) { num_curse += 1; }
			if (state.deck[player][i] == estate) { num_estate +=1; }
			if (state.deck[player][i] == duchy) { num_duchy += 1; }
			if (state.deck[player][i] == province) { num_province +=1; }
			if (state.deck[player][i] == great_hall) {num_great_hall += 1; }
			if (state.deck[player][i] == gardens) { num_garden += 1; }
		}
		
		num_cards = state.deckCount[player] + state.handCount[player] + 
					state.discardCount[player];
		calculated = num_curse*(-1) + num_estate + num_duchy*3 + 
					 num_province*6 + num_great_hall + num_garden*(num_cards / 10);
		
		if(calculated != scoreFor(player, &state))
            fail(0);
	}
	
	printf("Passed.\n");
	return 0;
}
